package cn.lyw.seckillgoods.service.service.impl;

import cn.common.beancopy.util.BeanCopierUtil;
import cn.common.redis.RedisServie;
import cn.common.restresult.util.RestResult;
import cn.common.restresult.util.ResultEnum;
import cn.hutool.core.lang.generator.ObjectIdGenerator;
import cn.lyw.seckillgoods.mapper.mapper.SeckillGoodsMapper;
import cn.lyw.seckillgoods.mapper.pojo.SmallMiSeckillGoods;
import cn.lyw.seckillgoods.service.bo.AddSeckillGoodsBO;
import cn.lyw.seckillgoods.service.bo.EditSeckillGoodsBO;
import cn.lyw.seckillgoods.service.bo.GetSeckillGoodsBySessionBO;
import cn.lyw.seckillgoods.service.domain.GetSeckillGoodsAllDO;
import cn.lyw.seckillgoods.service.domain.GetSeckillGoodsByIdDO;
import cn.lyw.seckillgoods.service.domain.GetSeckillGoodsBySessionDO;
import cn.lyw.seckillgoods.service.service.SeckillGoodsService;
import cn.lyw.seckillsession.service.domain.GetSessionAllDO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

/**
 * @Author   lyw
 * @Description 秒杀商品逻辑层
 * @Date 9:35 2021/7/24
 * @Param
 * @return
 */
@Service
public class SeckillGoodsServiceImpl implements SeckillGoodsService {

    @Autowired
    private SeckillGoodsMapper seckillGoodsMapper;

    @Autowired
    private RedisServie redisServie;


    /**
     * @Author   lyw
     * @Description  查询全部秒杀商品
     * @Date 9:38 2021/7/24
     * @Param
     * @return
     */
    @Override
    public List<GetSeckillGoodsAllDO> getSeckillGoodsAll() {

        //调用查询全部方法
        List<SmallMiSeckillGoods> smallMiSeckillGoods = seckillGoodsMapper.selectAll();

        //转换返回值类型
        List<GetSeckillGoodsAllDO> getSeckillGoodsAllDOS = BeanCopierUtil.copyList(smallMiSeckillGoods, GetSeckillGoodsAllDO.class);

        return getSeckillGoodsAllDOS;
    }


    /**
     * @Author   lyw
     * @Description 根据秒杀场次id查询秒商品
     * @Date 10:38 2021/7/24
     * @Param
     * @return
     */
    @Override
    public List<GetSeckillGoodsBySessionDO> getSeckillGoodsBySession(GetSeckillGoodsBySessionBO bo) {

        //转换参数
        SmallMiSeckillGoods smallMiSeckillGoods = BeanCopierUtil.copyObj(bo, SmallMiSeckillGoods.class);

        //调用查询
        List<SmallMiSeckillGoods> select = seckillGoodsMapper.select(smallMiSeckillGoods);

        //转换返回值
        List<GetSeckillGoodsBySessionDO> getSeckillGoodsBySessionDOS = BeanCopierUtil.copyList(select, GetSeckillGoodsBySessionDO.class);

        return getSeckillGoodsBySessionDOS;
    }


    /**
     * @Author   lyw
     * @Description  根据id查询秒杀商品
     * @Date 10:55 2021/7/24
     * @Param
     * @return
     */
    @Override
    public GetSeckillGoodsByIdDO getSeckillGoodsById(String id) {

        //调用查询
        SmallMiSeckillGoods smallMiSeckillGoods = seckillGoodsMapper.selectByPrimaryKey(id);

        //转换参数
        GetSeckillGoodsByIdDO getSeckillGoodsByIdDO = BeanCopierUtil.copyObj(smallMiSeckillGoods, GetSeckillGoodsByIdDO.class);

        return getSeckillGoodsByIdDO;
    }


    /**
     * @Author   lyw
     * @Description 修改秒杀商品
     * @Date 11:01 2021/7/24
     * @Param
     * @return
     */
    @Override
    public RestResult editSeckillGoods(EditSeckillGoodsBO bo) {

        //转换参数
        SmallMiSeckillGoods smallMiSeckillGoods = BeanCopierUtil.copyObj(bo, SmallMiSeckillGoods.class);

        //校验数据
        //数量
        Integer seckillCount = smallMiSeckillGoods.getSeckillCount();
        if (seckillCount!=null){
            if (seckillCount<0){
                return new RestResult(ResultEnum.EDIT_ERROR);
            }
        }

        //修改时间
        smallMiSeckillGoods.setModifyDate(new Date());

        //版本号
        SmallMiSeckillGoods smallMiSeckillGoods1 = seckillGoodsMapper.selectByPrimaryKey(smallMiSeckillGoods.getId());
        Integer oldVersion = new Integer(smallMiSeckillGoods1.getVersion());
        Integer newVersion=(oldVersion + 1);

        smallMiSeckillGoods.setVersion(newVersion.toString());


        //调用修改方法
        int i = seckillGoodsMapper.updateByPrimaryKeySelective(smallMiSeckillGoods);

        //判断返回值
        if(i==0){
            return new RestResult(ResultEnum.EDIT_ERROR);
        }
        return new RestResult(ResultEnum.EDIT_SUCCESS);

    }


    /**
     * @Author   lyw
     * @Description 删除秒杀商品
     * @Date 11:21 2021/7/24
     * @Param
     * @return
     */
    @Override
    public RestResult delSeckillGoods(String id) {

        //调用删除
        int i = seckillGoodsMapper.deleteByPrimaryKey(id);

        //判断返回值
        if (i==0){
            return new RestResult(ResultEnum.DEL_ERROR);
        }

        return new RestResult(ResultEnum.DEL_SUCCESS);
    }


    /**
     * @Author   lyw
     * @Description 添加秒杀商品
     * @Date 11:30 2021/7/24
     * @Param
     * @return
     */
    @Override
    public RestResult addSeckillGoods(AddSeckillGoodsBO bo) {

        //转换参数
        SmallMiSeckillGoods smallMiSeckillGoods = BeanCopierUtil.copyObj(bo, SmallMiSeckillGoods.class);

        //自动生成id
        ObjectIdGenerator objectIdGenerator = new ObjectIdGenerator();
        String next = objectIdGenerator.next();
        smallMiSeckillGoods.setId(next);

        //判断spu_id是否存在(在consumer判断)

        //判断sku_id是否存在(在consumer判断)

        //判断数量不能小于等于0
        if (smallMiSeckillGoods.getSeckillCount()<=0){
            return new RestResult(ResultEnum.ADD_ERROR);
        }

        //创建时间，修改时间，版本号
        smallMiSeckillGoods.setCreationDate(new Date());
        smallMiSeckillGoods.setModifyDate(new Date());
        smallMiSeckillGoods.setVersion("1");

        //调用添加方法
        int i = seckillGoodsMapper.insertSelective(smallMiSeckillGoods);

        //判断返回值
        if (i==0){
            return new RestResult(ResultEnum.ADD_ERROR);
        }

        return new RestResult(ResultEnum.ADD_SUCCESS);

    }
}
